package com.mission.schedule.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.res.AssetFileDescriptor
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Handler
import android.os.Vibrator
import com.mission.schedule.constants.ShareFile
import com.mission.schedule.cutimage.Log
import com.mission.schedule.utils.SharedPrefUtil
import java.util.*


/**
 * 整点半点服务
 */
class MTimeReceiver: BroadcastReceiver() {

    private lateinit var sharep: SharedPrefUtil

    override fun onReceive(context: Context, intent: Intent?) {

        synchronized(context) {

            Log.e("MTimeReceiver","MTimeReceiver-Start")

            sharep = SharedPrefUtil(context, ShareFile.USERFILE)
            val zebadiah = sharep.getString(context, ShareFile.USERFILE, ShareFile.ZDTX, "")
            val badman = sharep.getString(context, ShareFile.USERFILE, ShareFile.BDTX, "")

            val cal = Calendar.getInstance()
            val hour = cal.get(Calendar.HOUR_OF_DAY)
            val min = cal.get(Calendar.MINUTE)
            if (min == 0) {
                if (!zebadiah.isEmpty()) {
                    val data = zebadiah.substring(0, zebadiah.length - 1).split(",")
                    data.map {
                        if (hour.toString() == it) {
                            alarmSong(hour.toString(), context)
                            return
                        }
                    }
                }
            } else if (min == 30) {
                if (!badman.isEmpty()) {
                    val data = badman.substring(0, badman.length - 1).split(",")
                    data.map {
                        if (hour.toString()+".5" == it) {
                            alarmSong(hour.toString()+".5", context)
                            return
                        }
                    }
                }
            }

            Log.e("MTimeReceiver","MTimeReceiver-Stop")
        }

    }

    private fun alarmSong(song: String, context: Context) {
        val istx = sharep.getString(context, ShareFile.USERFILE, ShareFile.ISTX, "1")
        if (istx == "0") {
            try {

                var vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
                val pattern = longArrayOf(100, 400) // 停止 开启 停止 开启
                vibrator.vibrate(pattern, -1) // 重复两次上面的pattern

                var fileDescriptor: AssetFileDescriptor = context.applicationContext.assets.openFd(song + ".mp3")
                var mediaPlayer = MediaPlayer()
                mediaPlayer.setDataSource(fileDescriptor.fileDescriptor, fileDescriptor.startOffset, fileDescriptor.length)
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
                mediaPlayer.prepare()
                mediaPlayer.start()
                Handler().postDelayed({ mediaPlayer.release() }, 5000)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}